package com.dxtan90.util;

import sun.misc.IOUtils;
import sun.nio.ch.IOUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author: shijunhao
 * @date: 2022/10/9 15:19
 * @description: 图片压缩
 */
public class ImageUtil {
    private static final int LINE_COUNT = 500;

    /**
     * @param image: 原图片
     * @return: java.awt.image.BufferedImage
     * @author: shijunhao
     * @date: 2022/10/9 15:30
     * @description: 图片抽帧缩小至700宽
     */
    public static BufferedImage zipImg(BufferedImage image) {
        if (image.getWidth() <= LINE_COUNT) {
            return image;
        }
        return resizeImage(image,LINE_COUNT,LINE_COUNT);
    }

    public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = resizedImage.createGraphics();
        graphics2D.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        graphics2D.dispose();
        return resizedImage;
    }
}
